/*
 * reading_files.cpp
 *
 *  Created on: 03/10/2011
 *      Authors: clayson celes e Andrey Bicalho
 */

#include <fstream>
#include <vector>
#include <cstring>
#include <iostream>
#include <sstream>

#include "CImg.h"


using namespace cimg_library;
using namespace std;


void split(const std::string & str, const string &delimiters, std::vector<std::string> * elems);
void getDataCube(const char* fileName, int x, int y, int z);
string insertNameFile(string nameFile, int value);


void split(const std::string & str, const string &delimiters, std::vector<std::string> * elems) {
	string::size_type lastPos = str.find_first_not_of(delimiters, 0);
	string::size_type pos = str.find_first_of(delimiters, lastPos);
	while (string::npos != pos || string::npos != lastPos) {
		elems->push_back(str.substr(lastPos, pos - lastPos));
		lastPos = str.find_first_not_of(delimiters, pos);
		pos = str.find_first_of(delimiters, lastPos);
	}
}

int main(int argc, char** argv){

	getDataCube("/home/clayson/mestrado/compnatural/tpd/hyperspectral_data", 145, 145, 200);


}

void getDataCube(const char* fileName, int x, int y, int z){

	CImg<double> cubo(x, y, 1, z);
	string fileChannelName;
	for(int index_channel_file = 0; index_channel_file < z; index_channel_file++){
		fileChannelName = insertNameFile(fileName, index_channel_file+1);
		ifstream infile;
		infile.open(fileChannelName.c_str());
		if(infile){
			string line;
			int j = 0;
			while(!infile.eof() && j < y){
				getline(infile, line);
				vector<string> vectorLine;
				split(line, "   ", &vectorLine);
				for(int i = 0; i < vectorLine.size(); i++){
					cubo(i, j, index_channel_file) = atof(vectorLine.at(i).c_str());
				}
				j++;
			}
		}
		infile.close();
	}

	//return cubo;

}


string insertNameFile(string nameFile, int value){
	//int pos = nameFile.rfind("/");
	//string file = nameFile.substr(0, pos);
	string file = nameFile;
	stringstream out;
	out << value;
	string z_string = out.str();
	file.append("/h_image_channel_").append(z_string).append(".txt");

	return file;
}
